package com.orange.wwwapi.security.handler;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.orange.core.pojo.R;
import com.orange.core.security.handler.AccountAuthenticationFailHandler;
import com.orange.core.util.RequestUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;

@Slf4j
public class WxAccountAuthenticationFailHandler extends AccountAuthenticationFailHandler<String> {

    public WxAccountAuthenticationFailHandler(ObjectMapper objectMapper) {
        super(objectMapper);
    }

    @SneakyThrows
    @Override
    protected R<String> getFailureR(AuthenticationException exception) {
        R<String> r = super.getFailureR(exception);
        WxMaJscode2SessionResult sessionResult = (WxMaJscode2SessionResult) RequestUtil.getRequest().getAttribute("wxUserInfo");
        r.setResult(super.objectMapper.writeValueAsString(sessionResult));
        return r;
    }
}
